package com.penghaisoft.microwms.product.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.dto.ProOrderInSnDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.BaseService;
import com.penghaisoft.microwms.common.wms.model.ProOrderInSn;
import com.penghaisoft.microwms.common.wms.model.ProStorageSn;
import com.penghaisoft.microwms.product.dao.ProOrderInSnMapper;
import com.penghaisoft.microwms.product.dao.ProStorageSnMapper;
import com.penghaisoft.microwms.product.service.IProOrderInSnService;

/**
 * @Description 成品下线入库明细表 service
 * @author luoteng
 * @date 2018年6月6日 下午2:35:15
 * @version 1.0
 */
@Service("proOrderInSnService")
public class ProOrderInSnServiceImpl extends BaseService implements
		IProOrderInSnService {
	@Resource
	private ProOrderInSnMapper proOrderInSnMapper;
	
	@Resource
	private ProStorageSnMapper proStorageSnMapper;

	/**
	 * 新增记录
	 * 
	 * @param baseResource
	 * @return
	 */
	@Override
	public Resp create(ProOrderInSn proOrderInSn) {
		proOrderInSnMapper.create(proOrderInSn);
		return success();
	}

	/**
	 * 删除记录（逻辑删除）
	 * 
	 * @param baseResource
	 * @return
	 */
	@Override
	public Resp delete(ProOrderInSn proOrderInSn) {
		proOrderInSnMapper.delete(proOrderInSn);
		return success();
	}

	/**
	 * 查询列表
	 * 
	 * @param pager
	 * @param baseResource
	 * @return
	 */
	@Override
	public Pager<ProOrderInSn> findListByCondition(int page, int rows,ProOrderInSn condition){
	    Pager<ProOrderInSn> pager = new Pager<>();
	    pager.setPage(page);
	    pager.setRows(rows);
		condition.preQuery();
		long size = proOrderInSnMapper.queryCount(condition);
		List<ProOrderInSn> records = new ArrayList<ProOrderInSn> ();
		if(size > 0){
			records = proOrderInSnMapper.queryList(pager,condition);
			
			ProOrderInSn sumAmount = proOrderInSnMapper.querySumAmount(condition);
			pager.setOrder("{\"amount\":\"" + sumAmount.getAmount() + "\"}");
		}
		pager.setRecords(records);
		pager.setTotalCount(size);
		return pager;
	}
	
	/** 
	 * @Title findModelList
	 * @Description mes给的下线型号
	 * @author luoteng
	 * @date 2018年6月13日:下午1:13:19
	 * @param page
	 * @param rows
	 * @param condition
	 * @return
	 */
	@Override
	public Pager<ProOrderInSn> findModelList(int page, int rows,ProOrderInSn condition){
	    Pager<ProOrderInSn> pager = new Pager<>();
	    pager.setPage(page);
	    pager.setRows(rows);
		condition.preQuery();
		long size = proOrderInSnMapper.queryModelCount(condition);
		List<ProOrderInSn> records = new ArrayList<ProOrderInSn> ();
		if(size > 0){
			records = proOrderInSnMapper.queryModelList(pager,condition);
		}
		pager.setRecords(records);
		pager.setTotalCount(size);
		return pager;
	}
	
	/** 
	* @Title: findModelListNew 
	* @Description: mes给的下线型号 （查询库存明细表 修改合同号的型号 也可以查出来）
	* @param @param page
	* @param @param rows
	* @param @param condition
	* @param @return  参数说明 
	* @return Pager<ProOrderInSn>    返回类型
	* @author luoteng 
	* @date 2018年11月8日 下午7:11:31 
	* @throws 
	*/
	@Override
	public Pager<ProStorageSn> findModelListNew(int page, int rows,ProStorageSn condition){
	    Pager<ProStorageSn> pager = new Pager<>();
	    pager.setPage(page);
	    pager.setRows(rows);
		condition.preQuery();
		long size = proStorageSnMapper.queryModelCount(condition);
		List<ProStorageSn> records = new ArrayList<ProStorageSn> ();
		if(size > 0){
			records = proStorageSnMapper.queryModelList(pager,condition);
		}
		pager.setRecords(records);
		pager.setTotalCount(size);
		return pager;
	}

	/**
	 * 查询单条
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public ProOrderInSn findById(String id) {
		return proOrderInSnMapper.queryById(id);
	}

	/**
	 * 修改记录
	 * 
	 * @param baseResource
	 * @return
	 */
	@Override
	public Resp update(ProOrderInSn proOrderInSn) {
		proOrderInSnMapper.updateBySelect(proOrderInSn);
		return success();
	}
	
	
	/** 
	 * @Title downlineScanBarcode
	 * @Description 下线扫码入库
	 * @author luoteng
	 * @date 2018年6月7日:上午10:08:12
	 * @param proOrderInSnDto
	 * @return
	 */
	@Transactional
	@Override
	public TransResult downlineScanBarcode(ProOrderInSnDto proOrderInSnDto){
		TransResult result = new TransResult();
        result.setCode(Constant.SUCCESS);
        
        Date now = new Date();
        
        ProOrderInSn proOrderInSn = new ProOrderInSn();
        proOrderInSn.setBarcode(proOrderInSnDto.getBarcode());
        proOrderInSn.setActiveFlag("1");
        
        List<ProOrderInSn> orderInSnBarList = proOrderInSnMapper.queryByAny(proOrderInSn);
        if(orderInSnBarList == null || orderInSnBarList.isEmpty()){
        	result.setCode(Constant.FAIL);
            result.setMsg("该条码未下线。");
            return result;
        }else if(orderInSnBarList.size() > 1){
        	result.setCode(Constant.FAIL);
            result.setMsg("该下线条码重复。");
            return result;
        }else{
        	ProOrderInSn tmp = orderInSnBarList.get(0);
//        	入库状态 0：创建 1：已下线入库
        	if(tmp.getBarcodeStatus() != null && tmp.getBarcodeStatus().equals("1")){
        		result.setCode(Constant.FAIL);
                result.setMsg("该条码已经下线入库。");
                return result;
        	}
        	
//        	入库状态 0：创建 1：已下线入库
        	tmp.setBarcodeStatus("1");
        	tmp.setWlCode(proOrderInSnDto.getWlCode());
        	tmp.setProInDate(now);
        	tmp.setLastModifiedBy(proOrderInSnDto.getLoginName());
        	tmp.setGmtModified(now);
        	
        	proOrderInSnMapper.updateBySelect(tmp);
        	
        	ProStorageSn proStorageSn = new ProStorageSn();
        	proStorageSn.setBarcode(tmp.getBarcode());
        	proStorageSn.setAmount(tmp.getAmount());
        	proStorageSn.setUnit(tmp.getUnit());
        	proStorageSn.setContractNo(tmp.getContractNo());
        	proStorageSn.setPurchaseNo(tmp.getPurchaseNo());
        	proStorageSn.setModel(tmp.getModel());
        	proStorageSn.setModelDesc(tmp.getModelDesc());
        	proStorageSn.setModelType(tmp.getModelType());
        	proStorageSn.setType(tmp.getType());
        	proStorageSn.setPrdOrder(tmp.getPrdOrder());
        	proStorageSn.setProManuDate(tmp.getProManuDate());
        	proStorageSn.setProPlanDate(tmp.getProPlanDate());
        	proStorageSn.setWlCode(proOrderInSnDto.getWlCode());
//        	0：可用 1：冻结 2：已出库 3：抽检冻结
        	proStorageSn.setBarcodeStatus("0");
        	proStorageSn.setCreateBy(proOrderInSnDto.getLoginName());
        	proStorageSn.setGmtCreate(now);
        	proStorageSn.setActiveFlag("1");
        	
        	proStorageSnMapper.create(proStorageSn);
        	
        	List<ProOrderInSn> rtnList = new ArrayList<ProOrderInSn>();
        	rtnList.add(tmp);
        	result.setList(rtnList);
        }
        
        return result;
	}
	
	/** 
	 * @Title downlineDelBarcode
	 * @Description 下线退扫【暂时不用】
	 * @author luoteng
	 * @date 2018年6月7日:上午10:08:20
	 * @param proOrderInSnDto
	 * @return
	 */
	@Override
	public TransResult downlineDelBarcode(ProOrderInSnDto proOrderInSnDto){
		TransResult result = new TransResult();
        result.setCode(Constant.SUCCESS);
        
        Date now = new Date();
        
        ProOrderInSn proOrderInSn = new ProOrderInSn();
        proOrderInSn.setBarcode(proOrderInSnDto.getBarcode());
        proOrderInSn.setActiveFlag("1");
        
        List<ProOrderInSn> orderInSnBarList = proOrderInSnMapper.queryByAny(proOrderInSn);
        if(orderInSnBarList == null || orderInSnBarList.isEmpty()){
        	result.setCode(Constant.FAIL);
            result.setMsg("该条码未下线。");
            return result;
        }else if(orderInSnBarList.size() > 1){
        	result.setCode(Constant.FAIL);
            result.setMsg("该下线条码重复。");
            return result;
        }else{
        	ProOrderInSn tmp = orderInSnBarList.get(0);
//        	入库状态 0：创建 1：已下线入库
        	if(tmp.getBarcodeStatus() != null && !tmp.getBarcodeStatus().equals("1")){
        		result.setCode(Constant.FAIL);
        		result.setMsg("该条码未下线入库。");
                return result;
        	}
        	
//        	入库状态 0：创建 1：已下线入库
        	tmp.setBarcodeStatus("0");
        	tmp.setLastModifiedBy(proOrderInSnDto.getLoginName());
        	tmp.setGmtModified(now);
        	
        	proOrderInSnMapper.updateBySelect(tmp);
        	
//        	ProStorageSn proStorageSn = new ProStorageSn();
//        	proStorageSn.setBarcode(tmp.getBarcode());
////        	1-有效 0-无效
//        	proStorageSn.setActiveFlag("0");
//        	
//        	proStorageSnMapper.create(proStorageSn);
        	
        	List<ProOrderInSn> rtnList = new ArrayList<ProOrderInSn>();
        	rtnList.add(tmp);
        	result.setList(rtnList);
        }
        
        return result;
	}
	
	/** 
	 * @Title mesBatchInsertOrderInSnCheck
	 * @Description 成品条码写入接口表前校验【MES用】
	 * @author luoteng
	 * @date 2018年6月20日:上午11:58:47
	 * @param list
	 * @return
	 */
	@Override
	public String mesBatchInsertOrderInSnCheck(List<ProOrderInSn> list){
		StringBuffer rtnMsg = new StringBuffer("");
		List<ProOrderInSn> rtnList = new ArrayList<ProOrderInSn>();
		HashSet<String> barSet = new HashSet<String>();
		for(ProOrderInSn tmp : list){
			if(tmp.getBarcode() == null || tmp.getBarcode().equals("")){
//				tmp.setRtnMsg("MES传递的该条码为空！");
				rtnMsg.append("MES传递的该条码(" + tmp.getBarcode() + ")为空！");
				rtnList.add(tmp);
			}else if(barSet.contains(tmp.getBarcode())){
//				tmp.setRtnMsg("MES传递的该条码重复！");
				rtnMsg.append("MES传递的该条码(" + tmp.getBarcode() + ")重复！");
				rtnList.add(tmp);
			}else{
				barSet.add(tmp.getBarcode());
				
				List<ProOrderInSn> snList = proOrderInSnMapper.queryByBarCode(tmp);
				if(snList != null && !snList.isEmpty()){
//					tmp.setRtnMsg("WMS内该条码已经下线！");
					rtnMsg.append("WMS内该条码(" + tmp.getBarcode() + ")已经下线！");
					rtnList.add(tmp);
				}
			}
		}
		return rtnMsg.toString();
	}
	
	/** 
	 * @Title mesBatchInsertOrderInSn
	 * @Description 成品条码写入接口表【MES用】
	 * @author luoteng
	 * @date 2018年6月20日:上午11:47:17
	 * @param list
	 * @return
	 */
	@Override
	@Transactional
	public Resp mesBatchInsertOrderInSn(List<ProOrderInSn> list){
		Date now = new Date();
		for(ProOrderInSn tmp : list){
//			生产时间
			tmp.setProManuDate(now);
			tmp.setGmtCreate(now);
			tmp.setCreateBy("MES");
			tmp.setActiveFlag("1");
//			入库状态 0：创建 1：已下线入库
			tmp.setBarcodeStatus("0");
			proOrderInSnMapper.create(tmp);
		}
		return success();
	}
	
	/** 
	 * @Title mesBatchInsertOrderInSn
	 * @Description 成品条码写入接口表【MES用】【下线即入库】
	 * @author luoteng
	 * @date 2018年6月20日:上午11:47:17
	 * @param list
	 * @return
	 */
	@Override
	@Transactional
	public Resp mesBatchInsertOrderInSnAuto(List<ProOrderInSn> list, String wlCode){
		Date now = new Date();
		for(ProOrderInSn tmp : list){
//			入库时间
			tmp.setProInDate(now);
			tmp.setWlCode(wlCode);
//			生产时间
			tmp.setProManuDate(now);
			tmp.setGmtCreate(now);
			tmp.setCreateBy("MES");
			tmp.setActiveFlag("1");
//			入库状态 0：创建 1：已下线入库
			tmp.setBarcodeStatus("1");
			proOrderInSnMapper.create(tmp);
			
			ProStorageSn proStorageSn = new ProStorageSn();
        	proStorageSn.setBarcode(tmp.getBarcode());
        	proStorageSn.setAmount(tmp.getAmount());
        	proStorageSn.setUnit(tmp.getUnit());
        	proStorageSn.setContractNo(tmp.getContractNo());
        	proStorageSn.setPurchaseNo(tmp.getPurchaseNo());
        	proStorageSn.setModel(tmp.getModel());
        	proStorageSn.setModelDesc(tmp.getModelDesc());
        	proStorageSn.setModelType(tmp.getModelType());
        	proStorageSn.setType(tmp.getType());
        	proStorageSn.setPrdOrder(tmp.getPrdOrder());
        	proStorageSn.setProManuDate(now);
        	proStorageSn.setProPlanDate(tmp.getProPlanDate());
        	proStorageSn.setWlCode(wlCode);
//        	0：可用 1：冻结 2：已出库 3：抽检冻结
        	proStorageSn.setBarcodeStatus("0");
//        	0：未质检 1：质检合格 2：质检不合格
        	proStorageSn.setCheckStatus("0");
        	proStorageSn.setCreateBy("MES_AUTO_IN");
        	proStorageSn.setGmtCreate(now);
        	proStorageSn.setActiveFlag("1");
        	
        	proStorageSnMapper.create(proStorageSn);
		}
		return success();
	}

	/* @Title: findListExp 
	 * @Description: 下线入库明细导出
	 * @author jzh
	 * @date 2018年10月8日 下午2:20:31 
	 */
	@Override
	public List<ProOrderInSn> findListExp(ProOrderInSn proOrderInSn) {
		// TODO Auto-generated method stub
		return proOrderInSnMapper.queryList(null,proOrderInSn);
	}
}
